# @Time : 2021/6/19 19:27
# @Author : Li Kunlun
# @Description : 测试类

import torch
import numpy as np

x = torch.Tensor([1, 2, 3, 4])  # torch.Tensor是默认的tensor类型（torch.FlaotTensor）的简称。

print('-' * 50)
print(x)  # tensor([1., 2., 3., 4.])
print(x.size())  # torch.Size([4])
print(x.dim())  # 1
print(x.numpy())  # [1. 2. 3. 4.]

print('-' * 50)
print(torch.unsqueeze(x, 0))  # tensor([[1., 2., 3., 4.]])
print(torch.unsqueeze(x, 0).size())  # torch.Size([1, 4])
print(torch.unsqueeze(x, 0).dim())  # 2
print(torch.unsqueeze(x, 0).numpy())  # [[1. 2. 3. 4.]]

print('-' * 50)
print(torch.unsqueeze(x, 1))
# tensor([[1.],
#         [2.],
#         [3.],
#         [4.]])
print(torch.unsqueeze(x, 1).size())  # torch.Size([4, 1])
print(torch.unsqueeze(x, 1).dim())  # 2

print("--------------------三维[[[]]]测试----------------------")
data_array = np.zeros((3, 5, 6))
data_array[0, 2, 2] = 4
data_array[1, 2, 2] = 1
# [[[0. 0. 0. 0. 0. 0.]
#   [0. 0. 0. 0. 0. 0.]
#   [0. 0. 4. 0. 0. 0.]
#   [0. 0. 0. 0. 0. 0.]
#   [0. 0. 0. 0. 0. 0.]]
#
#  [[0. 0. 0. 0. 0. 0.]
#   [0. 0. 0. 0. 0. 0.]
#   [0. 0. 1. 0. 0. 0.]
#   [0. 0. 0. 0. 0. 0.]
#   [0. 0. 0. 0. 0. 0.]]
#
#  [[0. 0. 0. 0. 0. 0.]
#   [0. 0. 0. 0. 0. 0.]
#   [0. 0. 0. 0. 0. 0.]
#   [0. 0. 0. 0. 0. 0.]
#   [0. 0. 0. 0. 0. 0.]]]
print(data_array)

print("--------------------unsqueeze测试----------------------")
a = torch.rand(1, 3)
"""
a-- tensor([[0.7852, 0.0185, 0.6578]])
a.shape-- torch.Size([1, 3])
"""
print("a--", a)
print("a.shape--", a.shape)

b1 = a.unsqueeze(0)
"""
b1-- tensor([[[0.7852, 0.0185, 0.6578]]])
b1.shape-- torch.Size([1, 1, 3])
"""
print("b1--", b1)
print("b1.shape--", b1.shape)

b2 = a.unsqueeze(1)
"""
b2-- tensor([[[0.7852, 0.0185, 0.6578]]])
b2.shape-- torch.Size([1, 1, 3])
"""
print("b2--", b2)
print("b2.shape--", b2.shape)

b3 = a.unsqueeze(2)
"""
b3-- tensor([[[0.7852],
         [0.0185],
         [0.6578]]])
b3.shape-- torch.Size([1, 3, 1])
"""
print("b3--", b3)
print("b3.shape--", b3.shape)

print("--------------------torch.unsqueeze(torch.linspace)----------------------")
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)
# tensor([[-1.0000],
#         [-0.9798],
#         [-0.9596],
#         [-0.9394],
#         [-0.9192],
#         [-0.8990],
#         [-0.8788],
#         [-0.8586],
print(x)
